package com.webdev.newtech.controller;

import com.webdev.newtech.domain.SysUser;
import com.webdev.newtech.utils.AjaxResult;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

//注册为控制器--向Spring注册一个bean
@RestController
@RequestMapping("test")
public class TestComtroller {

    @GetMapping("auth")
    public String auth(){
        return "hello world!";
    }

    @GetMapping("login")
    public String login(String userName,String password){
        return "接收到了你的参数，userName:"+userName+" password:"+password;
//        return "hello world!";
    }

    /**
     * 只能处理 get 请求
     * @param user
     * @return
     */
    @GetMapping("user")
    public String user(SysUser user){
        return "user:"+user.toString();
    }

    /**
     * 只能处理 post 请求
     * @param userName
     * @param password
     * @return
     * 当传递的参数和接收参数的变量不一致的时候，可以使用requestParam进行映射 name => userName
     */
    @PostMapping("login")
    public String plogin(@RequestParam(value = "xinmin",required = false) String userName,String password){
        return "接收到了你的post参数，name:"+userName+" password:"+password;
//        return "hello world!";
    }

    /**
     * 处理任意的action
     * @return
     */
    @RequestMapping("data")
    public AjaxResult data(){

        SysUser user = new SysUser();
        user.setUserName("接收到符合");
        user.setAge(18);
        user.setGender("M");
        SysUser user2 = new SysUser();
        user2.setUserName("张师傅");
        user2.setAge(88);
        user2.setGender("F");
        List list = new ArrayList();
        list.add(user);
        list.add(user2);
        return AjaxResult.success("success",list);
    }

    /**
     * 路径占位符 {var}
     * @param userId
     * @return
     */
    @PostMapping("{userId}/delete")
    public String deleteById(@PathVariable("userId") Integer userId){
        return "想要删除id为:" + userId+ " 的数据";
    }

}
